<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import { reactive, ref } from "vue";
import { getRepairList, getResidentialList, handleRepair } from "@/api/property";
import { ElNotification } from "element-plus";

defineOptions({
  name: "repairList"
});
const form = reactive({
  name: ""
});
const loading = ref(false);
const residentialList = ref([]);
const dataList = ref([]);
const residential = ref();
const onSearch = () => {
  getResidentialList().then(res => {
    residentialList.value = res.data.records;
  });
  loading.value = false;
};
//获取报修列表
const getRepair = () => {
  getRepairList({ name: form.name, residentialId: residential.value }).then(res => {
    dataList.value = res.data;
  });
};
//处理报修
const repair = (data?:any)=>{
  data.status=1;
  handleRepair(data).then(res=>{
    if (res.code === 200)
      ElNotification({
        title: "Success",
        message: res.msg,
        type: "success"
      });
    else
      ElNotification({
        title: "Error",
        message: res.msg,
        type: "error"
      });
    getRepair();
  })
}
//删除报修记录
const deleteRepair = (data?:any) =>{

}
onSearch();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="报修内容：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入报修内容"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="getRepair"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
      <br />
      <el-form-item label="归属小区">
        <el-radio-group
          v-model="residential"
          size="small"
          @change="getRepair();"
        >
          <el-radio-button
            v-for="list in residentialList"
            :label="list.id"
            :value="list.id"
          >{{ list.name }}
          </el-radio-button
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-table :data="dataList.records" table-layout="fixed">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="region" label="报修区域" />
      <el-table-column prop="address" label="报修地址" />
      <el-table-column label="报修状态" >
        <template #default="scope">
          <el-tag type="warning" v-if="scope.row.status===0">待维修</el-tag>
          <el-tag type="success" v-if="scope.row.status===1">已维修</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="报修时间" />
      <el-table-column prop="finish" label="维修时间" />
      <el-table-column prop="operatorId" label="维修人员" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.status===1"
            @click="deleteRepair(scope.row.id)"
          >删除
          </el-button>
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.status===0"
            @click="repair(scope.row)"
          >维修
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>
